package com.example.myapplication22;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication22.OnclickImplements.AiHao;
import com.example.myapplication22.utils.MyHelper;

import java.util.Calendar;
import java.util.Random;

public class Register extends AppCompatActivity {
    /*public void a(){
        //this.getFragmentManager();
       super.getFragmentManager();
       this.g
    }*/

    private String[] items=new String[]{"篮球","足球","羽毛球"};
    private boolean[] booleans=new boolean[]{true,false,false};
    AlertDialog dialog1=null;
   private ImageView iv;
    //存放选择的图片
    private int ivNum=0;
    private int ivN=R.drawable.t01;

    Button btn_tx,fanhui;

    private int[] ivId=new int[]{
            R.drawable.t01,R.drawable.t02,R.drawable.t03,
            R.drawable.t04,R.drawable.t05,R.drawable.dui
    };
    //性别
    int index = 0;
    String[] xb = new String[]{"男", "女"};
    AlertDialog dialog = null;
    //爱好
    StringBuilder stringBuilder = new StringBuilder();
    //账号
   private String userId;
    //密码
   private String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
       /* MyHelper myHelper = new MyHelper(Register.this);
        SQLiteDatabase w = myHelper.getWritableDatabase();

        ContentValues content = new ContentValues();
        content.put("userId","123");
        content.put("password","123");
        long l = w.insert("user", null, content);
        System.out.println(l);
        w.close();*/

        //选择头像
        btn_tx = findViewById(R.id.btn_tx);
        btn_tx.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String[] s = new String[ivId.length];
                for (int i = 0; i < ivId.length; i++) {
                    s[i] = ivId[i] + "";
                }
                AlertDialog.Builder b = new AlertDialog.Builder(Register.this)
                        .setTitle("选择头像")
                        .setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                ivNum = i;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                iv.setImageResource(ivId[ivNum]);
                                ivN=ivId[ivNum];//存放当前选择图片
                                Toast.makeText(Register.this, "成功", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        });
                dialog = b.create();
                dialog.show();
            }
        });


        final TextView xbText = findViewById(R.id.xbText);
        //设置性别

        findViewById(R.id.xb).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this)
                        .setTitle("设置性别")
                        .setSingleChoiceItems(new String[]{"男", "女"}, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                index = i;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                xbText.setText(xb[index]);
                                dialog.dismiss();//关闭对话框
                            }

                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialog.dismiss();
                            }
                        });
                        dialog = builder.create();
                        dialog.show();

            }
        });

        //爱好点击事件
        findViewById(R.id.ah).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this)
                        .setTitle("请添加兴趣爱好")
                        .setMultiChoiceItems(items, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                //设置是否选中
                                booleans[i] = b;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //循环
                                for (int a = 0; a < booleans.length; a++) {
                                    if (booleans[a]) {
                                        stringBuilder.append(items[a] + " ");
                                    }
                                }
                                //Toast显示
                                Toast.makeText(Register.this, stringBuilder, Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        });
                dialog = builder.create();
                dialog.show();
            }
        });

        //日期控件
       final DatePicker date = findViewById(R.id.date1);
       //获取到的日期
         int year1 = date.getYear();
         int month1 = date.getMonth();
         int day1 = date.getDayOfMonth();
        //
        Calendar c = Calendar.getInstance();
         final int year = c.get(Calendar.YEAR);
         final int month = c.get(Calendar.MONTH);
         final int day = c.get(Calendar.DATE);
        //更换日期 Toast提示
        date.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                Toast.makeText(Register.this,
                        i+ " 年"+i1+" 月"+i2+"日 " ,
                        Toast.LENGTH_LONG).show();
            }
        });//
        //返回键 监听事件
        final  Button register = findViewById(R.id.register);
      final   EditText u=findViewById(R.id.userId);
      final EditText p=findViewById(R.id.passWord);
      //注册按钮
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(Register.this, Login.class);
            //存数据
                Bundle bundle = new Bundle();
                bundle.putInt("iv",ivId[ivNum]);//头像数据
                //账号
                bundle.putString("userId",(u.getText()+""));
                userId=u.getText().toString();
                //密码
                bundle.putString("password",p.getText()+"");
                password=p.getText().toString();
                //性别
                bundle.putString("xb",xb[index]);
                //爱好
                intent.putExtras(bundle);

                //添加注册数据
                SQLiteDatabase value = new MyHelper(Register.this).getWritableDatabase();
                ContentValues content = new ContentValues();
                content.put("userId",userId);
                content.put("password",password);
                content.put("img",ivN);
                content.put("sex",xb[index]);
                content.put("date",year+"-"+month+"-"+day);
                long l = value.insert("user", null, content);
                value.close();
                System.err.println(l);
                System.out.println("注册成功");
                //Toast.makeText(Register.this,"success",Toast.LENGTH_SHORT);

                startActivity(intent);

            }
        });
        //返回
         fanhui = findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Register.this, Login.class);
                Bundle bundle = new Bundle();
                bundle.putString("tag","1");
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
        });
        //图片点击事件
        iv = findViewById(R.id.iv1);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //iv.setImageDrawable();
                int i = new Random().nextInt(ivId.length);
                iv.setImageResource(ivId[i]);
                ivN=ivId[i];//存放当前选择的图片
                ivNum=i;
            }
        });

    }





}
